//
//  UserServer.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/3.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "UserServer.h"
#import "UpLoadImageTool.h"

@implementation UserServer
- (void)registerUserComplete:(void(^)(BOOL success,UserInfo*info,NSString*msg))complete{
    if (!self.nickname.length || !self.password.length || !self.re_password.length) {
        return;
    }
    NSMutableDictionary*parameters = [NSMutableDictionary dictionary];
    if (self.choseImage) {
        [UpLoadImageTool uploadImage:self.choseImage size:CGSizeMake(100, 100) complete:^(NSString * imageUrl, NSString * errorMsg) {
            if (!errorMsg) {
                [parameters setObject:imageUrl forKey:@"headimgurl"];
                [parameters setObject:self.nickname forKey:@"nickname"];
                [parameters setObject:self.password forKey:@"password"];
                [parameters setObject:self.re_password forKey:@"re_password"];
                [NetworkManager POST:URL_user parameters:parameters responseCache:nil success:^(id responseObject) {
                    XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
                    if ([response.code isEqualToNumber:@200]) {
                        UserInfo*info = [UserInfo modelWithJSON:response.data];
                        !complete?:complete(YES,info,response.message);
                    }else{
                        !complete?:complete(NO,nil,response.message);
                    }
                } failure:^(NSError *error) {
                    
                }];
            }else{
                !complete?:complete(NO,nil,errorMsg);
            }
        }];
    }else{
        [parameters setObject:self.nickname forKey:@"nickname"];
        [parameters setObject:self.password forKey:@"password"];
        [parameters setObject:self.re_password forKey:@"re_password"];
        [NetworkManager POST:URL_user parameters:parameters responseCache:nil success:^(id responseObject) {
            XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
            if ([response.code isEqualToNumber:@200]) {
                UserInfo*info = [UserInfo modelWithDictionary:response.data];
                !complete?:complete(YES,info,response.message);
            }else{
                !complete?:complete(NO,nil,response.message);
            }
        } failure:^(NSError *error) {
            
        }];
    }
}

- (void)loginComplete:(void(^)(BOOL success,NSString*msg))complete{
    NSString *push_alias = [NSString stringWithFormat:@"ios_%@",[RegularExp getRandomStringWithNum:32]];
    if (!self.account.length || !self.password.length) {
        return;
    }
    [NetworkManager GET:URL_user_login parameters:@{@"account":self.account,@"password":self.password,@"push_alias":push_alias} responseCache:nil success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            [kUserManager LoginSuccess:response.data completion:^(BOOL success, NSInteger code, NSString *des) {
                if (success) {
                    !complete?:complete(YES,des);
                }else{
                    !complete?:complete(NO,des);
                }
            }];
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        !complete?:complete(NO,kNetworkError);
    }];
}

- (void)getAccountComplete:(void(^)(BOOL success,NSString*msg,NSString*account))complete{
    if (!self.private_key.length) {
        return;
    }
    [NetworkManager GET:URL_user_get_account parameters:@{@"private_key":self.private_key} responseCache:nil success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            if (StrValid(response.data)) {
                !complete?:complete(YES,response.message,response.data);
            }else{
                !complete?:complete(NO,response.message,nil);
            }
        }else{
            !complete?:complete(NO,response.message,nil);
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)resetAccountPwdComplete:(void(^)(BOOL success,NSString*msg))complete{
    if (!self.private_key.length || !self.password.length || !self.re_password.length || !self.account.length) {
        return;
    }
    [NetworkManager PUT:URL_user_forget_password parameters:@{@"password":self.password,@"re_password":self.re_password,@"account":self.account,@"private_key":self.private_key} success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            !complete?:complete(YES,response.message);
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)saveAccountComplete:(void(^)(BOOL success,NSString*msg))complete{
    if (!self.action.length || !self.value.length) {
        return;
    }
    [NetworkManager PUT:URL_user_save_usermsg parameters:@{@"action":self.action,@"value":self.value} success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            !complete?:complete(YES,response.message);
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)changePasswordAccountComplete:(void (^)(BOOL, NSString * _Nonnull))complete{
    if (!self.old_password.length || !self.password.length ||!self.re_password.length) {
        return;
    }
    [NetworkManager PUT:URL_user_change_password parameters:@{@"old_password":self.old_password,@"password":self.password,@"re_password":self.re_password} success:^(id responseObject) {
        XHBaseModel *response = [XHBaseModel modelWithDictionary:responseObject];
        if ([response.code isEqualToNumber:@200]) {
            !complete?:complete(YES,response.message);
        }else{
            !complete?:complete(NO,response.message);
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)dealloc{
    
}
@end
